/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carpool.services;

import carpool.HibernateUtil;
import carpool.persistence.Utilizadores;
import java.beans.*;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author sdinis
 */
public class UserBean implements Serializable {

    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
    private String sampleProperty;
    private PropertyChangeSupport propertySupport;

    public UserBean() {
        propertySupport = new PropertyChangeSupport(this);
    }

    public boolean createAndStoreUser(Utilizadores user) {
        Session session=null;
        try {
            session = HibernateUtil.getCarpoolSessionFactory().getCurrentSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();

            return true;
        } catch (HibernateException ex) {
            Logger.getLogger(UserBean.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
        }
    }

    public Utilizadores getUser(String login) {

        Utilizadores u;
            Session session = HibernateUtil.getCarpoolSessionFactory().getCurrentSession();//.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            u = (Utilizadores) (session.createQuery("from Utilizadores as utilizador where utilizador.login=?").setString(0, login).uniqueResult());
            session.getTransaction().commit();
            return u;
    }

    public String getSampleProperty() {
        return sampleProperty;
    }

    public void setSampleProperty(String value) {
        String oldValue = sampleProperty;
        sampleProperty = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
}
